import {
	request
} from '@/utils/request';

class SearchCondition {
	constructor(arg) {
		// 项目
		this.techAreas = []; //技术领域
		this.applicationAreas = []; //应用领域
		this.achievementTypes = []; //成果类型

		// 服务
		this.serviceTypes = []; // 服务类型

		// 活动
		this.activityTypes = []; //活动类型
		this.activityIndustries = []; //活动行业
		
		// 专家
		this.researchAreas = []; //研究领域
		
		this.project_query_conditions = {};
		this.activity_search_conditions = {};
		this.service_search_conditions = {};
	}
	
	loadExpert = ()=>{
		request.get({
			url: '/manager/Research/selectAllResearch',
			data: {},
		}).then(res => {
			console.log('loadExpert', res)
			this.researchAreas = res;
		}, res => {});
	}

	loadProject = () => {
		//应用领域
		this.queryApplicationArea();
		// 技术领域
		this.queryTechArea();
		//成果类型
		this.queryFruitType();
	}

	loadService = () => {
		//服务类型
		this.queryServiceType();
	}

	loadAct = () => {
		//活动类型
		this.queryActTypes();
		//活动行业
		this.queryActIndustry();
	}

	queryActIndustry = () => {
		request.get({
			url: 'dict/getTreeByType',
			data: {
				type: 'activities_industry'
			},
		}).then(res => {
			this.activityIndustries = res.children;
		}, res => {});
	}

	queryActTypes = () => {
		request.get({
			url: 'dict/getTreeByType',
			data: {
				type: 'activities_type'
			},
		}).then(res => {
			this.activityTypes = res.children;
		}, res => {});
	}

	queryServiceType = () => {
		request.get({
			url: 'servicType/selectServiceType',
			data: {
				type: 'fruit_type'
			},
		}).then(res => {
			this.serviceTypes = res.data.children;
		}, res => {});
	}

	queryFruitType = () => {
		request.get({
			url: 'dict/getTreeByType',
			data: {
				type: 'fruit_type'
			},
		}).then(res => {
			this.achievementTypes = res.children;
		}, res => {});
	}

	queryTechArea = () => {
		request.get({
			url: 'dict/getTreeByType',
			data: {
				type: 'skill_area'
			},
		}).then(res => {
			this.techAreas = res.children || [];
		}, res => {});
	}

	queryApplicationArea = () => {
		request.get({
			url: 'dict/getTreeByType',
			data: {
				type: 'activities_industry'
			},
		}).then(res => {
			this.applicationAreas = res.children || [];
		}, res => {});
	}

	//组装 查询条件 type:[project,activity,service]
	composeCondition = (keyword, type = 'project') => {
		if (type === 'project') {
			return this.projectCondition(keyword);
		} else if (type === 'activity') {
			return this.activityCondition(keyword);
		} else if (type === 'service') {
			return this.serviceCondition(keyword);
		}else if (type === 'expert') {
			return this.expertCondition(keyword);
		}
		return {};
	}

	// 项目 匹配算法
	projectCondition = keyword => {
		// this.techAreas = []; //技术领域
		// this.applicationAreas = []; //应用领域
		// this.achievementTypes = []; //成果类型
		if(keyword==''){
			return {
				keyWord: keyword,
			};
		}
		let searchCode = null;
		for (let tech of this.techAreas) {
			const value = tech.value;
			if (value.indexOf(keyword) !== -1 || keyword.indexOf(value) !== -1) {
				searchCode = tech.code;
			}
			if (tech.children) {
				for (let tech of tech.children) {
					const value = tech.value;
			
					if (value.indexOf(keyword) !== -1 || keyword.indexOf(value) !== -1) {
						searchCode = tech.code;
					}
				}
			}
		}
		// for (let application of this.applicationAreas) {
		// 	const value = application.value;
		// 	if(value.indexOf(keyword) !== -1 || keyword.indexOf(value) !==-1){
		// 		return application.code;
		// 	}
		// }
		// for (let achievement of this.achievementTypes) {
		// 	const value = achievement.value;
		// 	if(value.indexOf(keyword) !== -1 || keyword.indexOf(value) !==-1){
		// 		return achievement.code;
		// 	}
		// }
		if (searchCode) {
			return {
				skillArea: [searchCode]
			}
		}
		return {
			keyWord: keyword,
		};
	}
	activityCondition = keyword => {
		if(keyword==''){
			return {
				keyWord: keyword,
			};
		}
		let searchCode = null;
		let params = {};
		for (let act of this.activityIndustries) {
			const value = act.value;
			if (value.indexOf(keyword) !== -1 || keyword.indexOf(value) !== -1) {
				params.activitiesIndustry = `${act.code}`
			}
		}
		for (let act of this.activityTypes) {
			const value = act.value;
			if (value.indexOf(keyword) !== -1 || keyword.indexOf(value) !== -1) {
				params.activitiesType = `${act.code}`
			}
		}
		if (params.activitiesIndustry || params.activitiesType) {
			return params;
		}
		return {
			keyword: keyword,
			sort: '',
			provinceId: '',
			cityId: '',
			activitiesType: '',
			activitiesIndustry: ''
		};

	}
	serviceCondition = keyword => {
		if(keyword==''){
			return {
				keyWord: keyword,
			};
		}
		let searchCode = null;
		for (let act of this.serviceTypes) {
			const value = act.value;
			if (value.indexOf(keyword) !== -1 || keyword.indexOf(value) !== -1) {
				searchCode = `${act.code}`
			}
		}
		if (searchCode) {
			return {
				skillArea: searchCode
			}
		}
		return {
			keyWord: keyword,
		};
	}
	
	expertCondition = keyword =>{
		if(keyword==''){
			return {
				keyWord: keyword,
			};
		}
		let searchCode = null;
		// console.log(' expertCondition keyword', keyword);
		
		for (let tech of this.researchAreas) {
			const value = tech.researchName;
			// console.log(' expertCondition value', value);
			if (value.indexOf(keyword) !== -1 || keyword.indexOf(value) !== -1) {
				searchCode = tech.code;
			}
		}
		// console.log(' expertCondition searchCode', searchCode);
		
		if (searchCode) {
			return {
				researchArea: searchCode
			}
		}
		return {
			keyWord: keyword,
		};
	}
}

const searchCondition = new SearchCondition();
export default searchCondition;
